package com.xdap.common.dao;

import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.processapi.pojo.entity.XdapProcInstance;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @USER liShuShu
 * @TIME 2023/2/14 0014 10:10
 * @DESC 流程
 **/
@Repository
@RequiredArgsConstructor
public class ComProcessInstanceDao {
    private final RuntimeDatasourceService runtimeDatasourceService;
    @Value("${apaas.single.tenantId}")
    private String tenantSchema;

    @Value("${apaas.single.appId}")
    private String appId;

    public static final String XDAP_PROC_INSTANCE = "xdap_proc_instance";

    public XdapProcInstance getXdapProcInstanceByDocId(String documentId) {
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery("xdap_activiti_" + tenantSchema);
        return mpaasQuery
                .table(XDAP_PROC_INSTANCE)
                .eq("document_id", documentId)
                .doQueryFirst(XdapProcInstance.class);
    }

    /**
     * @Description
     * @Date 2023/10/24 10:09
     * @Author shangsheng.li
     **/
    public List<XdapProcInstance> queryApprovingByDocId(String documentId) {
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery("xdap_activiti_" + tenantSchema);
        return mpaasQuery
                .table(XDAP_PROC_INSTANCE)
                .eq("status", "APPROVING")
                .eq("document_id", documentId)
                .doQuery(XdapProcInstance.class);
    }

    /**
     * @Description
     * @Date 2023/10/24 10:09
     * @Author shangsheng.li
     **/
    public List<XdapProcInstance> queryApprovingAll() {
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery("xdap_activiti_" + tenantSchema);
        // 防止推送到其他的应用下，此处处理的时候，需要带上appId
        return mpaasQuery
                .table(XDAP_PROC_INSTANCE)
                .eq("app_id",appId)
                .eq("status", "APPROVING")
                .doQuery(XdapProcInstance.class);
    }

}
